package cn.mugutu.finance.service.impl;

import cn.mugutu.finance.domain.DoubleBall;
import cn.mugutu.finance.enums.DoubleBallNumberType;
import cn.mugutu.finance.service.DoubleBallService;
import cn.mugutu.finance.service.system.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 双色球
 *
 * @author gan
 * @date 2018/12/12 2:07 PM
 */

@Service
public class DoubleBallServiceImpl extends BaseServiceImpl<DoubleBall> implements DoubleBallService {


    @Override
    public Integer getMaxPeriod() {
        return (Integer) getUniqueResult("SELECT MAX(o.period) FROM DoubleBall o ");
    }

    /**
     * 查询最近一期开奖号码
     */
    @Override
    public String getLastOpenNumber() {
        Integer maxPeriod = getMaxPeriod();
        List<DoubleBall> list = findByHql("SELECT o FROM DoubleBall o " +
                "where o.period = ? and o.type = ? ", maxPeriod, DoubleBallNumberType.ACTUAL);
        if (list == null || list.size() != 1) {
            return "获取失败.";
        }
        DoubleBall doubleBall = list.get(0);
        StringBuilder lastOpenNumber = new StringBuilder("上期开奖: <span style=\"font-size: 20px;color: red\">")
                .append(doubleBall.getRed1()).append("  ")
                .append(doubleBall.getRed2()).append("  ")
                .append(doubleBall.getRed3()).append("  ")
                .append(doubleBall.getRed4()).append("  ")
                .append(doubleBall.getRed5()).append("  ")
                .append(doubleBall.getRed6()).append("  ")
                .append("</span>")
                .append("<span style=\"font-size: 20px;color: blue\">")
                .append(doubleBall.getBlueNumber())
                .append("</span>");
        return lastOpenNumber.toString();
    }

    /**
     * 查询最近一期推荐号码
     */
    @Override
    public String getLastOpenNumberRecomment() {
        Integer maxPeriod = getMaxPeriod();
        List<DoubleBall> list = findByHql("SELECT o FROM DoubleBall o " +
                "where o.period = ? and o.type = ? ", maxPeriod, DoubleBallNumberType.RECOMMEND);

        if (list == null || list.size() != 1) { //没有生成就立马生成
            //TODO 推荐算法
            Object uniqueResult = getUniqueResult("select count(o) from DoubleBall o group by o.red1");
            System.out.println("uniqueResult========================>" + uniqueResult);
        }

        DoubleBall doubleBall = list.get(0);
        StringBuilder lastOpenNumber = new StringBuilder("上期推荐: <span style=\"font-size: 20px;color: red\">")
                .append(doubleBall.getRed1()).append("  ")
                .append(doubleBall.getRed2()).append("  ")
                .append(doubleBall.getRed3()).append("  ")
                .append(doubleBall.getRed4()).append("  ")
                .append(doubleBall.getRed5()).append("  ")
                .append(doubleBall.getRed6()).append("  ")
                .append("</span>")
                .append("<span style=\"font-size: 20px;color: blue\">")
                .append(doubleBall.getBlueNumber())
                .append("</span>");
        return lastOpenNumber.toString();
    }


    /**
     * 获取下一期开奖系统推荐号码
     */
    @Override
    public String getNextRecomment() {
        //TODO 生成系统推荐算法


        Integer maxPeriod = getMaxPeriod();
        List<DoubleBall> list = findByHql("SELECT o FROM DoubleBall o " +
//                "where o.period = ? and o.type = ? ", maxPeriod, DoubleBallNumberType.RECOMMEND);
                "where o.period = ? and o.type = ? ", maxPeriod, DoubleBallNumberType.ACTUAL);

        if (list == null || list.size() != 1) { //没有生成就立马生成
            //TODO 推荐算法
        }

        DoubleBall doubleBall = list.get(0);
        StringBuilder lastOpenNumber = new StringBuilder("下期推荐: <u><span style=\"font-size: 20px;color: red\">")
                .append(doubleBall.getRed1()).append("  ")
                .append(doubleBall.getRed2()).append("  ")
                .append(doubleBall.getRed3()).append("  ")
                .append(doubleBall.getRed4()).append("  ")
                .append(doubleBall.getRed5()).append("  ")
                .append(doubleBall.getRed6()).append("  ")
                .append("</span>")
                .append("<span style=\"font-size: 20px;color: blue\">")
                .append(doubleBall.getBlueNumber())
                .append("</span></u>");
        return lastOpenNumber.toString();
    }

}
